package com.bittech.facerecognition.web;

import com.bittech.facerecognition.model.Response;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Author: secondriver
 * Created: 2019/5/28
 */
@WebServlet(urlPatterns = {"/upload"})
@MultipartConfig
public class UploadFileServlet extends ImageBaseServlet {
    
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String result = writeImage(req, "faceImage");
        if (result == null) {
            responseJSON(resp, Response.ofFailed("", "上传文件失败，稍后重试"));
        } else {
            String contextPath = req.getServletContext().getContextPath();
            String url;
            if (!contextPath.isEmpty()) {
                url = contextPath + "/show?id=" + result;
            } else {
                url = "show?id=" + result;
            }
            responseJSON(resp, Response.ofOk(url));
        }
    }
}
